﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using UCenter.Core.Model;

namespace UCenter.Models
{
    [Table(Name = "dbo.Users")]
    public class User : EntityBase
    {
        #region Private Fields

        private int _Id;

        private string _email;

        private string _password;

        private string _salt;

        private short _status;

        private bool _isOnline;

        private DateTime _createDate;

        private DateTime? _verifyDate;

        private DateTime? _lastLoginDate;

        private EntitySet<VerifyCode> _verifyCodes;

        private EntitySet<FriendGroup> _friendGroups;

        private EntitySet<Friendship> _friendships;

        private EntitySet<History> _histories;

        private EntityRef<Profile> _profile;

        private EntitySet<Resume> _resumes;

        private EntitySet<UserModuleMap> _userModuleMaps;

        #endregion

        #region Constructor

        public User()
        {
            this._verifyCodes = new EntitySet<VerifyCode>(new Action<VerifyCode>(this.AttachVerifyCodes), new Action<VerifyCode>(this.DetachVerifyCodes));
            this._friendGroups = new EntitySet<FriendGroup>(new Action<FriendGroup>(this.AttachFriendGroups), new Action<FriendGroup>(this.DetachFriendGroups));
            this._friendships = new EntitySet<Friendship>(new Action<Friendship>(this.AttachFriendships1), new Action<Friendship>(this.DetachFriendships1));
            this._histories = new EntitySet<History>(new Action<History>(this.AttachHistories), new Action<History>(this.DetachHistories));
            this._profile = default(EntityRef<Profile>);
            this._resumes = new EntitySet<Resume>(new Action<Resume>(this.AttachResumes), new Action<Resume>(this.DetachResumes));
            this._userModuleMaps = new EntitySet<UserModuleMap>(new Action<UserModuleMap>(this.AttachUserModuleMaps), new Action<UserModuleMap>(this.DetachUserModuleMaps));
        }

        #endregion

        #region Public Properties

        [Column(Name = "ID", Storage = "_Id", AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
        public int ID
        {
            get
            {
                return this._Id;
            }
            set
            {
                if ((this._Id != value))
                {
                    this.SendPropertyChanging("ID");
                    this._Id = value;
                    this.SendPropertyChanged("ID", value);
                }
            }
        }

        [Column(Name = "Email", Storage = "_email", DbType = "VarChar(128) NOT NULL", CanBeNull = false)]
        public string Email
        {
            get
            {
                return this._email;
            }
            set
            {
                if ((this._email != value))
                {
                    this.SendPropertyChanging("Name");
                    this._email = value;
                    this.SendPropertyChanged("Name", value);
                }
            }
        }

        [Column(Name = "Password", Storage = "_password", DbType = "VarChar(128) NOT NULL", CanBeNull = false)]
        public string Password
        {
            get
            {
                return this._password;
            }
            set
            {
                if ((this._password != value))
                {
                    this.SendPropertyChanging("Password");
                    this._password = value;
                    this.SendPropertyChanged("Password", value);
                }
            }
        }

        [Column(Name = "Salt", Storage = "_salt", DbType = "VarChar(32)")]
        public string Salt
        {
            get
            {
                return this._salt;
            }
            set
            {
                if ((this._salt != value))
                {
                    this.SendPropertyChanging("Salt");
                    this._salt = value;
                    this.SendPropertyChanged("Salt", value);
                }
            }
        }

        [Column(Name = "Status", Storage = "_status", DbType = "TinyInt NOT NULL")]
        public short Status
        {
            get
            {
                return this._status;
            }
            set
            {
                if ((this._status != value))
                {
                    this.SendPropertyChanging("Status");
                    this._status = value;
                    this.SendPropertyChanged("Status", value);
                }
            }
        }

        [Column(Name = "Online", Storage = "_isOnline", DbType = "Bit NOT NULL")]
        public bool IsOnline
        {
            get
            {
                return this._isOnline;
            }
            set
            {
                if ((this._isOnline != value))
                {
                    this.SendPropertyChanging("IsOnline");
                    this._isOnline = value;
                    this.SendPropertyChanged("IsOnline", value);
                }
            }
        }

        [Column(Name = "CreateDate", Storage = "_createDate", DbType = "DateTime NOT NULL")]
        public DateTime CreateDate
        {
            get
            {
                return this._createDate;
            }
            set
            {
                if ((this._createDate != value))
                {
                    this.SendPropertyChanging("CreateDate");
                    this._createDate = value;
                    this.SendPropertyChanged("CreateDate", value);
                }
            }
        }

        [Column(Name = "VerifyDate", Storage = "_verifyDate", DbType = "DateTime")]
        public DateTime? VerifyDate
        {
            get
            {
                return this._verifyDate;
            }
            set
            {
                if ((this._verifyDate != value))
                {
                    this.SendPropertyChanging("VerifyDate");
                    this._verifyDate = value;
                    this.SendPropertyChanged("VerifyDate", value);
                }
            }
        }

        [Column(Name = "LastLoginDate", Storage = "_lastLoginDate", DbType = "DateTime")]
        public DateTime? LastLoginDate
        {
            get
            {
                return this._lastLoginDate;
            }
            set
            {
                if ((this._lastLoginDate != value))
                {
                    this.SendPropertyChanging("LastLoginDate");
                    this._lastLoginDate = value;
                    this.SendPropertyChanged("LastLoginDate", value);
                }
            }
        }

        [Association(Name = "User_VerifyCode", Storage = "_verifyCodes", OtherKey = "UserId")]
        public EntitySet<VerifyCode> VerifyCodes
        {
            get
            {
                return this._verifyCodes;
            }
            set
            {
                this._verifyCodes.Assign(value);
            }
        }

        [Association(Name = "User_FriendGroup", Storage = "_friendGroups", OtherKey = "UserId")]
        public EntitySet<FriendGroup> FriendGroups
        {
            get
            {
                return this._friendGroups;
            }
            set
            {
                this._friendGroups.Assign(value);
            }
        }

        [Association(Name = "User_Friendship", Storage = "_friendships", OtherKey = "UserId")]
        public EntitySet<Friendship> Friendships
        {
            get
            {
                return this._friendships;
            }
            set
            {
                this._friendships.Assign(value);
            }
        }

        [Association(Name = "User_History", Storage = "_histories", OtherKey = "UserId")]
        public EntitySet<History> Histories
        {
            get
            {
                return this._histories;
            }
            set
            {
                this._histories.Assign(value);
            }
        }

        [Association(Name = "User_Profile", Storage = "_profile", ThisKey = "ID", IsUnique = true, IsForeignKey = false)]
        public Profile Profile
        {
            get
            {
                return this._profile.Entity;
            }
            set
            {
                Profile previousValue = this._profile.Entity;
                if (((previousValue != value)
                            || (this._profile.HasLoadedOrAssignedValue == false)))
                {
                    if ((previousValue != null))
                    {
                        this._profile.Entity = null;
                        previousValue.User = null;
                    }
                    this._profile.Entity = value;
                    if ((value != null))
                    {
                        value.User = this;
                    }
                }
            }
        }

        [Association(Name = "User_Resume", Storage = "_resumes", OtherKey = "UserId")]
        public EntitySet<Resume> Resumes
        {
            get
            {
                return this._resumes;
            }
            set
            {
                this._resumes.Assign(value);
            }
        }

        [Association(Name = "User_UserModuleMap", Storage = "_userModuleMaps", OtherKey = "UserId")]
        public EntitySet<UserModuleMap> UserModuleMaps
        {
            get
            {
                return this._userModuleMaps;
            }
            set
            {
                this._userModuleMaps.Assign(value);
            }
        }

        #endregion

        #region Attach and Detach Methods

        private void AttachVerifyCodes(VerifyCode entity)
        {
            entity.User = this;
        }

        private void DetachVerifyCodes(VerifyCode entity)
        {
            entity.User = null;
        }

        private void AttachFriendGroups(FriendGroup entity)
        {
            entity.User = this;
        }

        private void DetachFriendGroups(FriendGroup entity)
        {
            entity.User = null;
        }

        private void AttachFriendships1(Friendship entity)
        {
            entity.User = this;
        }

        private void DetachFriendships1(Friendship entity)
        {
            entity.User = null;
        }

        private void AttachHistories(History entity)
        {
            entity.User = this;
        }

        private void DetachHistories(History entity)
        {
            entity.User = null;
        }

        private void AttachResumes(Resume entity)
        {
            entity.User = this;
        }

        private void DetachResumes(Resume entity)
        {
            entity.User = null;
        }

        private void AttachUserModuleMaps(UserModuleMap entity)
        {
            entity.User = this;
        }

        private void DetachUserModuleMaps(UserModuleMap entity)
        {
            entity.User = null;
        }

        #endregion

        #region Object overrides

        public override string ToString()
        {
            return this.Profile.TrueName;
        }

        #endregion
    }
}
